﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class VRSceneMove : MonoBehaviour {
    public SteamVR_TrackedController controller;
    public GameObject VRCamera;
    public GameObject VRCameraRig;
    public float speed;
    Vector3 dir;

    public float ScaleWithHeightConstant;
    public Vector3 CameraRigInitScale;
    public float CameraRigInitPosY;

    public float lowestPosY;


    private void Start()
    {
        CameraRigInitScale = VRCameraRig.transform.localScale;
        CameraRigInitPosY = VRCameraRig.transform.position.y;
    }

    private void Update()
    {
        var axis = controller.controllerState.rAxis0;
        dir = (VRCamera.transform.localToWorldMatrix * new Vector3(axis.x, 0, axis.y)).normalized;
        if (controller.padPressed)
        {
            float realSpeed = speed * (VRCameraRig.transform.localScale.magnitude / CameraRigInitScale.magnitude);
            VRCameraRig.transform.Translate(dir * realSpeed * Time.deltaTime,Space.World);
        }
        VRCameraRig.transform.position=VRCameraRig.transform.position.y < lowestPosY ?
            VRCameraRig.transform.position + new Vector3(0, lowestPosY-VRCameraRig.transform.position.y,0) : VRCameraRig.transform.position;


        Vector3 toScale = CameraRigInitScale *
            (1 + ScaleWithHeightConstant * (CameraRigInitPosY-VRCameraRig.transform.position.y ) / CameraRigInitPosY);
        VRCameraRig.transform.localScale = toScale.sqrMagnitude > 1?toScale: VRCameraRig.transform.localScale;
    }
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawRay(VRCamera.transform.position, dir);
    }


}
